﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using TomShane.Neoforce.Controls;

namespace Warlock.Menu
{
    class MultiPlayerConnectScreen : IGameScreen
    {
        public Game1 MainGame { get; set; }

        Window window;

        TextBox ipTextBox;
        TextBox portTextBox;
        Button connectButton;
        Button ServerstartButton;

        public void Initialize(GraphicsDeviceManager graphics, ContentManager content)
        {
            MainGame = (Game1)Game1.Manager.Game;

            window = new Window(Game1.Manager);
            window.Init();
            window.Text = "";
            window.Width = graphics.PreferredBackBufferWidth;
            window.Height = graphics.PreferredBackBufferHeight;
            window.Left = 0;
            window.Top = 0;
            window.Movable = false;

            ipTextBox = new TextBox(Game1.Manager);
            ipTextBox.Init();
            ipTextBox.Parent = window;
            ipTextBox.Left = 40;
            ipTextBox.Top = 60;
            ipTextBox.Width = 100;
            ipTextBox.Height = 20;
            ipTextBox.Text = "127.0.0.1";

            portTextBox = new TextBox(Game1.Manager);
            portTextBox.Init();
            portTextBox.Parent = window;
            portTextBox.Left = ipTextBox.Left + ipTextBox.Width + 20;
            portTextBox.Top = ipTextBox.Top;
            portTextBox.Height = ipTextBox.Height;
            portTextBox.Width = 80;
            portTextBox.Text = "7536";

            connectButton = new Button(Game1.Manager);
            connectButton.Init();
            connectButton.Parent = window;
            connectButton.Text = "Mit IP verbinden";
            connectButton.Left = portTextBox.Left + portTextBox.Width + 20;
            connectButton.Top = portTextBox.Top;
            connectButton.Width = 180;
            connectButton.Height = portTextBox.Height;
            connectButton.Click += new TomShane.Neoforce.Controls.EventHandler(connectButton_Click);
            connectButton = new Button(Game1.Manager);

            ServerstartButton = new Button(Game1.Manager);
            ServerstartButton.Init();
            ServerstartButton.Parent = window;
            ServerstartButton.Text = "Server Starten";
            ServerstartButton.Left = portTextBox.Left + portTextBox.Width + 20;
            ServerstartButton.Top = portTextBox.Top + 30;
            ServerstartButton.Width = 180;
            ServerstartButton.Height = portTextBox.Height;
            ServerstartButton.Click += new TomShane.Neoforce.Controls.EventHandler(ServerstartButton_Click);

            Game1.Manager.Add(window);
        }

        void ServerstartButton_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            Netcom.Server server = new Netcom.Server();
            Warlock.Properties.Settings.Default.Gamemode = "MultiplayerS";
            Warlock.GameStuff.MainGameScreen mainGameScreen = new Warlock.GameStuff.MainGameScreen(MainGame);
            MainGame.SwitchGamescreen(mainGameScreen);
        }

        void connectButton_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            if (!IsIPCorrect(ipTextBox.Text))
            {
                MessageBox.Show("Ungueltige IP-Addresse eingegeben.", "Ungueltige Eingabe!");
                return;          //Hier evtl noch eine Fehlermeldung (MessageBox)
            }
            if (!IsPortCorrect(portTextBox.Text))
            {
                MessageBox.Show("Ungueltiger Port.", "Ungueltige Eingabe!");
                return;          //Hier evtl noch eine Fehlermeldung (MessageBox)
            }
            Warlock.Properties.Settings.Default.Gamemode = "Multiplayer";
            Warlock.Properties.Settings.Default.ConIP = ipTextBox.Text;
            Warlock.GameStuff.MainGameScreen MGS = new Warlock.GameStuff.MainGameScreen(MainGame);
            MainGame.SwitchGamescreen(MGS);
            //TODO: Jeremias
        }

        public void UnloadContent()
        {
            Game1.Manager.Remove(window);
            window.Dispose();
        }

        public void Update(GameTime gameTime)
        {
            Game1.Manager.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            Game1.Manager.Draw(gameTime);
        }

        static bool IsIPCorrect(string ip)
        {
            Regex ipRegex = new Regex("\\b(?<IPAddress>(2([0-4][0-9]|5[0-5])|[01]?[0-9]{1,2})(\\.(2([0-4][0-9]|5[0-5])|[01]?[0-9]{1,2})){3})\\b");
            return ipRegex.IsMatch(ip);
        }
        static bool IsPortCorrect(string sPort)
        {
            int port;
            if (!int.TryParse(sPort, out port))
                return false;
            if (port > 0)
                return true;
            return false;
        }
    }
}